@{
    ViewData["Title"] = "系统设置"; 
    Layout = "_Layout";
}

<div class="layui-container-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <h3>系统设置</h3>
                </div>
                <div class="layui-card-body">
                    <div class="layui-row layui-col-space15">
                        <div class="layui-col-md6">
                            <!-- AI模型配置 -->
                            <div class="layui-card">
                                <div class="layui-card-header">
                                    <h5>AI模型配置</h5>
                                </div>
                                <div class="layui-card-body">
                                    <form class="layui-form" id="aiConfigForm">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">OpenAI API密钥</label>
                                            <div class="layui-input-block">
                                                <input type="password" class="layui-input" id="apiKey" placeholder="请输入API密钥">
                                                <div class="layui-form-mid layui-word-aux">您的OpenAI API密钥将用于AI问答功能</div>
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">模型选择</label>
                                            <div class="layui-input-block">
                                                <select class="layui-select" id="model">
                                                    <option value="gpt-3.5-turbo">GPT-3.5 Turbo</option>
                                                    <option value="gpt-4">GPT-4</option>
                                                    <option value="gpt-4-turbo">GPT-4 Turbo</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">最大Token数</label>
                                            <div class="layui-input-block">
                                                <input type="number" class="layui-input" id="maxTokens" value="2000" min="100" max="4000">
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">温度</label>
                                            <div class="layui-input-block">
                                                <input type="range" class="layui-range" id="temperature" min="0" max="2" step="0.1" value="0.7">
                                                <div class="layui-form-mid layui-word-aux">0 = 确定性回答，2 = 创造性回答</div>
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <button type="button" class="layui-btn layui-btn-normal" onclick="saveAIConfig()">
                                                    <i class="fas fa-save"></i> 保存AI配置
                                                </button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-col-md6">
                            <!-- 系统配置 -->
                            <div class="layui-card">
                                <div class="layui-card-header">
                                    <h5>系统配置</h5>
                                </div>
                                <div class="layui-card-body">
                                    <form class="layui-form" id="systemConfigForm">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">最大文件大小(MB)</label>
                                            <div class="layui-input-block">
                                                <input type="number" class="layui-input" id="maxFileSize" value="50" min="1" max="100">
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">文档分块大小</label>
                                            <div class="layui-input-block">
                                                <input type="number" class="layui-input" id="chunkSize" value="1000" min="100" max="5000">
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">分块重叠大小</label>
                                            <div class="layui-input-block">
                                                <input type="number" class="layui-input" id="chunkOverlap" value="200" min="0" max="1000">
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">向量维度</label>
                                            <div class="layui-input-block">
                                                <input type="number" class="layui-input" id="vectorDimension" value="1536" min="512" max="4096">
                                            </div>
                                        </div>
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <button type="button" class="layui-btn layui-btn-normal" onclick="saveSystemConfig()">
                                                    <i class="fas fa-save"></i> 保存系统配置
                                                </button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-row layui-col-space15" style="margin-top: 15px;">
                        <div class="layui-col-md12">
                            <!-- 系统状态 -->
                            <div class="layui-card">
                                <div class="layui-card-header">
                                    <h5>系统状态</h5>
                                    <button class="layui-btn layui-btn-xs layui-btn-primary" onclick="refreshSystemStatus()">
                                        <i class="fas fa-refresh"></i> 刷新
                                    </button>
                                </div>
                                <div class="layui-card-body">
                                    <div class="layui-row layui-col-space15">
                                        <div class="layui-col-md3">
                                            <div class="layui-text-center">
                                                <h4 id="documentCount">0</h4>
                                                <p class="layui-text-gray">知识库文档</p>
                                            </div>
                                        </div>
                                        <div class="layui-col-md3">
                                            <div class="layui-text-center">
                                                <h4 id="sessionCount">0</h4>
                                                <p class="layui-text-gray">活跃会话</p>
                                            </div>
                                        </div>
                                        <div class="layui-col-md3">
                                            <div class="layui-text-center">
                                                <h4 id="apiStatus">正常</h4>
                                                <p class="layui-text-gray">API状态</p>
                                            </div>
                                        </div>
                                        <div class="layui-col-md3">
                                            <div class="layui-text-center">
                                                <h4 id="dbStatus">正常</h4>
                                                <p class="layui-text-gray">数据库状态</p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-row layui-col-space15" style="margin-top: 15px;">
                        <div class="layui-col-md12">
                            <!-- 系统操作 -->
                            <div class="layui-card">
                                <div class="layui-card-header">
                                    <h5>系统操作</h5>
                                </div>
                                <div class="layui-card-body">
                                    <div class="layui-row layui-col-space15">
                                        <div class="layui-col-md4">
                                            <button type="button" class="layui-btn layui-btn-warm layui-btn-lg layui-btn-fluid" onclick="rebuildIndex()">
                                                <i class="fas fa-sync"></i> 重建索引
                                            </button>
                                            <div class="layui-form-mid layui-word-aux">重新处理所有文档的向量索引</div>
                                        </div>
                                        <div class="layui-col-md4">
                                            <button type="button" class="layui-btn layui-btn-normal layui-btn-lg layui-btn-fluid" onclick="exportData()">
                                                <i class="fas fa-download"></i> 导出数据
                                            </button>
                                            <div class="layui-form-mid layui-word-aux">导出知识库和会话数据</div>
                                        </div>
                                        <div class="layui-col-md4">
                                            <button type="button" class="layui-btn layui-btn-danger layui-btn-lg layui-btn-fluid" onclick="clearData()">
                                                <i class="fas fa-trash"></i> 清空数据
                                            </button>
                                            <div class="layui-form-mid layui-word-aux">清空所有文档和会话数据</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        layui.use(['form', 'layer'], function(){
            var form = layui.form;
            var layer = layui.layer;
            
            // 页面加载完成后执行
            loadConfigurations();
            refreshSystemStatus();
        });

        // 加载配置
        function loadConfigurations() {
            // 这里可以从API加载配置，暂时使用默认值
            console.log('加载配置...');
        }

        // 保存AI配置
        function saveAIConfig() {
            const config = {
                apiKey: $('#apiKey').val(),
                model: $('#model').val(),
                maxTokens: parseInt($('#maxTokens').val()),
                temperature: parseFloat($('#temperature').val())
            };

            // 这里可以发送到API保存配置
            console.log('保存AI配置:', config);
            layui.use('layer', function(){
                var layer = layui.layer;
                layer.msg('AI配置保存成功', {icon: 1});
            });
        }

        // 保存系统配置
        function saveSystemConfig() {
            const config = {
                maxFileSize: parseInt($('#maxFileSize').val()),
                chunkSize: parseInt($('#chunkSize').val()),
                chunkOverlap: parseInt($('#chunkOverlap').val()),
                vectorDimension: parseInt($('#vectorDimension').val())
            };

            // 这里可以发送到API保存配置
            console.log('保存系统配置:', config);
            layui.use('layer', function(){
                var layer = layui.layer;
                layer.msg('系统配置保存成功', {icon: 1});
            });
        }

        // 刷新系统状态
        function refreshSystemStatus() {
            // 模拟加载系统状态
            $('#documentCount').text('15');
            $('#sessionCount').text('3');
            $('#apiStatus').text('正常').css('color', 'green');
            $('#dbStatus').text('正常').css('color', 'green');
            
            layui.use('layer', function(){
                var layer = layui.layer;
                layer.msg('系统状态已刷新', {icon: 1});
            });
        }

        // 重建索引
        function rebuildIndex() {
            if (confirm('确定要重建索引吗？这可能需要一些时间。')) {
                layui.use('layer', function(){
                    var layer = layui.layer;
                    layer.msg('开始重建索引...', {icon: 16, time: 0});
                    
                    // 模拟重建过程
                    setTimeout(function() {
                        layer.closeAll();
                        layer.msg('索引重建完成', {icon: 1});
                    }, 3000);
                });
            }
        }

        // 导出数据
        function exportData() {
            layui.use('layer', function(){
                var layer = layui.layer;
                layer.msg('开始导出数据...', {icon: 16, time: 0});
                
                // 模拟导出过程
                setTimeout(function() {
                    layer.closeAll();
                    layer.msg('数据导出完成', {icon: 1});
                }, 2000);
            });
        }

        // 清空数据
        function clearData() {
            if (confirm('确定要清空所有数据吗？此操作不可恢复！')) {
                layui.use('layer', function(){
                    var layer = layui.layer;
                    layer.msg('开始清空数据...', {icon: 16, time: 0});
                    
                    // 模拟清空过程
                    setTimeout(function() {
                        layer.closeAll();
                        layer.msg('数据清空完成', {icon: 1});
                        $('#documentCount').text('0');
                        $('#sessionCount').text('0');
                    }, 2000);
                });
            }
        }
    </script>
} 
